package cn.edu.ncu.housinghub.mapper;

import cn.edu.ncu.housinghub.entity.maintance.Maintenance;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 维修申请数据访问层
 */
@Mapper
public interface MaintenanceMapper {
    
    /**
     * 插入维修申请
     */
    @Insert("INSERT INTO maintenance (tenantId, houseId, faultDescription, maintenanceStatus, faultPhoto, createTime) " +
            "VALUES (#{user.id}, #{house.houseId}, #{faultDescription}, #{maintenanceStatus}, #{faultPhoto}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "maintenanceId")
    int insertMaintenance(Maintenance maintenance);
    
    /**
     * 根据租客ID查询维修申请列表
     */
    @Select("SELECT m.maintenanceId, m.tenantId, m.houseId, m.faultDescription, m.maintenanceStatus, " +
            "m.faultPhoto, m.createTime, h.houseSummary, h.houseLocation " +
            "FROM maintenance m " +
            "LEFT JOIN house h ON m.houseId = h.houseId " +
            "WHERE m.tenantId = #{tenantId} " +
            "ORDER BY m.createTime DESC")
    @Results({
        @Result(property = "maintenanceId", column = "maintenanceId"),
        @Result(property = "user.id", column = "tenantId"),
        @Result(property = "house.houseId", column = "houseId"),
        @Result(property = "house.houseSummary", column = "houseSummary"),
        @Result(property = "house.houseLocation", column = "houseLocation"),
        @Result(property = "faultDescription", column = "faultDescription"),
        @Result(property = "maintenanceStatus", column = "maintenanceStatus"),
        @Result(property = "faultPhoto", column = "faultPhoto"),
        @Result(property = "createTime", column = "createTime")
    })
    List<Maintenance> selectByTenantId(Long tenantId);
    
    /**
     * 根据房东ID查询维修申请列表
     */
    @Select("SELECT m.maintenanceId, m.tenantId, m.houseId, m.faultDescription, m.maintenanceStatus, " +
            "m.faultPhoto, m.createTime, h.houseSummary, h.houseLocation, u.username " +
            "FROM maintenance m " +
            "LEFT JOIN house h ON m.houseId = h.houseId " +
            "LEFT JOIN user u ON m.tenantId = u.userId " +
            "WHERE h.landLordId = #{landlordId} " +
            "ORDER BY m.createTime DESC")
    @Results({
        @Result(property = "maintenanceId", column = "maintenanceId"),
        @Result(property = "user.id", column = "tenantId"),
        @Result(property = "user.username", column = "username"),
        @Result(property = "house.houseId", column = "houseId"),
        @Result(property = "house.houseSummary", column = "houseSummary"),
        @Result(property = "house.houseLocation", column = "houseLocation"),
        @Result(property = "faultDescription", column = "faultDescription"),
        @Result(property = "maintenanceStatus", column = "maintenanceStatus"),
        @Result(property = "faultPhoto", column = "faultPhoto"),
        @Result(property = "createTime", column = "createTime")
    })
    List<Maintenance> selectByLandlordId(Long landlordId);
    
    /**
     * 根据ID查询维修申请详情
     */
    @Select("SELECT m.maintenanceId, m.tenantId, m.houseId, m.faultDescription, m.maintenanceStatus, " +
            "m.faultPhoto, m.createTime, h.houseSummary, h.houseLocation, u.username " +
            "FROM maintenance m " +
            "LEFT JOIN house h ON m.houseId = h.houseId " +
            "LEFT JOIN user u ON m.tenantId = u.userId " +
            "WHERE m.maintenanceId = #{maintenanceId}")
    @Results({
        @Result(property = "maintenanceId", column = "maintenanceId"),
        @Result(property = "user.id", column = "tenantId"),
        @Result(property = "user.username", column = "username"),
        @Result(property = "house.houseId", column = "houseId"),
        @Result(property = "house.houseSummary", column = "houseSummary"),
        @Result(property = "house.houseLocation", column = "houseLocation"),
        @Result(property = "faultDescription", column = "faultDescription"),
        @Result(property = "maintenanceStatus", column = "maintenanceStatus"),
        @Result(property = "faultPhoto", column = "faultPhoto"),
        @Result(property = "createTime", column = "createTime")
    })
    Maintenance selectById(Long maintenanceId);
    
    /**
     * 更新维修申请状态
     */
    @Update("UPDATE maintenance SET maintenanceStatus = #{status} WHERE maintenanceId = #{maintenanceId}")
    int updateMaintenanceStatus(@Param("maintenanceId") Long maintenanceId, 
                               @Param("status") Integer status, 
                               @Param("remark") String remark);
    
    /**
     * 检查租客是否有权限对该房源申请维修
     */
    @Select("SELECT COUNT(*) > 0 FROM contract c " +
            "WHERE c.tenantId = #{tenantId} AND c.houseId = #{houseId} " +
            "AND c.signStatus = 5 AND c.endDate >= CURDATE()")
    boolean checkTenantPermission(@Param("tenantId") Long tenantId, @Param("houseId") Long houseId);
    
    /**
     * 检查房东是否有权限处理该维修申请
     */
    @Select("SELECT COUNT(*) > 0 FROM maintenance m " +
            "LEFT JOIN house h ON m.houseId = h.houseId " +
            "WHERE m.maintenanceId = #{maintenanceId} AND h.landLordId = #{landlordId}")
    boolean checkLandlordPermission(@Param("landlordId") Long landlordId, @Param("maintenanceId") Long maintenanceId);
}
